<template>
  <div>
    <el-dialog
      title="代理"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :show-close="true"
      :close-on-press-escape="false"
    >
      <el-form :model="agent" :rules="rules" label-position="right" ref="form">
        <el-form-item label="手机号" prop="phoneNum">
          <el-input v-model="agent.phoneNum" type="number" @blur="getCustomerByPhoneNum" />
        </el-form-item>
        <el-alert v-if="phoneNumberTips" :title="phoneNumberTips" type="warning" :closable="false"></el-alert>
        <!-- <el-form-item label="代理人id" prop="customerId">
          <el-input v-model="agent.customerId" type="number" :disabled="true" />
        </el-form-item>-->
        <el-form-item label="姓名" prop="name">
          <el-input v-model="agent.name" />
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="agent.address" />
        </el-form-item>
        <el-form-item label="支付金额" prop="fee">
          <el-input v-model="agent.fee" type="number" min="0" />
        </el-form-item>
        <el-form-item label="代理余额" prop="balance">
          <el-input v-model="agent.balance" type="number" min="0" />
        </el-form-item>
        <el-form-item label="代理来源类型" prop="sourceType">
          <el-select v-model="agent.sourceType" placeholder="请选择" clearable>
            <el-option
              v-for="item in sourceTypeOptions"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代理来源类型值" prop="sourceId">
          <el-select
            filterable
            remote
            clearable
            :remote-method="remoteMethod"
            :loading="loading"
            v-model="agent.sourceId"
            placeholder="请输入关键词"
          >
            <el-option
              v-for="item in sourceIdOptions"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证正面" prop="card1">
          <singleImgUpload :url.sync="agent.card1" dir="agentCard"></singleImgUpload>
        </el-form-item>
        <el-form-item label="身份证反面" prop="card2">
          <singleImgUpload :url.sync="agent.card2" dir="agentCard"></singleImgUpload>
        </el-form-item>
        <div v-if="agent.customerId">
          <label>关联微信信息:</label>
          <el-row style="margin: 10px 5px;">
            <el-col :span="12">
              <el-image :src="agent.avatarUrl" style="width:60px;height:60px;" />
            </el-col>
            <el-col :span="12">昵称:{{ agent.nickname }}</el-col>
          </el-row>
          <el-row style="margin: 10px 5px;">
            <el-col :span="12">性别:{{ agent.sex==1?'男':'女' }}</el-col>
            <el-col :span="12">宣传总人数:{{ agent.recommendNum }}</el-col>
          </el-row>
          <el-row style="margin: 10px 5px;">
            <el-col :span="12">累计消费:{{ agent.allTotal }}</el-col>
            <el-col :span="12">累计积分:{{ agent.totalCredits }}</el-col>
          </el-row>
          <el-row style="margin: 10px 5px;">
            <el-col :span="12">积分余额:{{ agent.availableCredits }}</el-col>
            <el-col :span="12">会员等级:{{ memberLevels[agent.memberLevel] }}</el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmClick">确 定</el-button>
        <el-button @click="cancelClick">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import singleImgUpload from "../../components/upload/SingleImgUpload.vue";

export default {
  // id即编辑的id，visible属性用来监听是否显示弹窗
  props: ["visible"],
  components: { singleImgUpload },
  watch: {},
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit("update:visible", value);
      },
    },
  },
  data() {
    return {
      // 弹窗相关属性
      dialogFieldBox: false,
      sourceTypeOptions: [],
      sourceIdOptions: [],
      agent: {
        id: null,
        phoneNum: null,
        customerId: null,
        card1: null,
        card2: null,
        name: null,
        address: null,
        balance: null,
        fee:null,
        sourceType: null,
        sourceId: null,
        avatarUrl: null,
        nickname: null,
        sex: null,
        recommendNum: null,
        allTotal: null,
        totalCredits: null,
        availableCredits: null,
        memberLevel: null,
      },
      // 表单校验
      rules: {
        phoneNum: [
          {
            required: true,
            message: "手机不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "名字不能为空",
            trigger: "blur",
          },
        ],
        balance: [
          {
            required: true,
            message: "支付金额不能为空",
            trigger: "blur",
          },
        ],
      },
      fee: 0,
      loading: false,
      phoneNumberTips: null,
      memberLevels: {
        0: "注册用户",
        1: "普通会员",
        2: "银卡会员",
        3: "金卡会员",
        4: "钻石会员",
      },
    };
  },
  methods: {
    initAgent(row) {
      this.sourceIdOptions = [];
      if (row) {
        this.agent = {
          id: row.id,
          phoneNum: row.phoneNum,
          customerId: row.customerId,
          card1: row.card1,
          card2: row.card2,
          name: row.name,
          address: row.address,
          balance: row.balance,
          fee:this.fee,
          sourceType: row.sourceType,
          sourceId: row.sourceId,
          avatarUrl: row.avatarUrl,
          nickname: row.nickname,
          sex: row.sex,
          recommendNum: row.recommendNum,
          allTotal: row.allTotal,
          totalCredits: row.totalCredits,
          availableCredits: row.availableCredits,
          memberLevel: row.memberLevel,
        };
        this.$https.get('agent/getAgentInfoFee',{id:row.id}).then(res=>{
          this.agent.fee=res.data.data.fee;
        })
        if (row.sourceId) {
          this.sourceIdOptions.push({
            key: row.sourceId,
            label: row.sourceName,
          });
        }
      } else {
        this.agent = {
          id: null,
          phoneNum: null,
          customerId: null,
          card1: null,
          card2: null,
          name: null,
          address: null,
          balance: this.fee,
          fee:this.fee,
          sourceType: null,
          sourceId: null,
          avatarUrl: null,
          nickname: null,
          sex: null,
          recommendNum: null,
          allTotal: null,
          totalCredits: null,
          availableCredits: null,
          memberLevel: null,
        };
      }
    },
    initOptions() {
      this.$https.get("agent/listSourceType", {}).then((res) => {
        this.sourceTypeOptions = res.data.data;
      });
      this.$https.get("agent/getAgentFee", {}).then((res) => {
        this.fee = res.data.data;
      });
    },
    // 确认保存
    confirmClick() {
      let param = { ...this.agent };
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url = "agent/addAgentInfo";
          if (param.id) {
            url = "agent/updateAgentInfo";
          }
          this.$https.postXform(url, param).then((res) => {
            this.$message({ message: "保存成功", type: "success" });
            this.dialogVisible = false;
            this.$emit("success");
          });
        } else {
          return false;
        }
      });
    },
    cancelClick() {
      this.dialogVisible = false;
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        this.$https
          .get("agent/searchSourceType", {
            name: query,
            sourceType: this.agent.sourceType,
          })
          .then((res) => {
            this.loading = false;
            this.sourceIdOptions = res.data.data;
          });
      } else {
        this.sourceIdOptions = [];
      }
    },
    getCustomerByPhoneNum() {
      this.$https
        .get("agent/getCustomerByPhoneNum", { phoneNum: this.agent.phoneNum })
        .then((res) => {
          let customer = res.data.data;
          if (!customer) {
            this.phoneNumberTips =
              "没有找到手机号为" +
              this.agent.phoneNum +
              "小程序客户,保存后将等待此手机号注册自动成为代理";
            return;
          }
          if (
            customer.userType === 4 &&
            customer.id !== this.agent.customerId
          ) {
            this.phoneNumberTips =
              "此手机号已是代理,代理人名称为：" + customer.name;
            return;
          }
          if (customer.userType === 2) {
            this.phoneNumberTips =
              "此手机号已是医生,医生名称为：" + customer.name;
            return;
          }
          if (this.agent.customerId && this.agent.customerId != customer.id) {
            this.phoneNumberTips =
              "原代理人资格将被取消，代理资格转移到：" + customer.name;
            return;
          }
          this.phoneNumberTips = "自动绑定到客户：" + customer.name;
        });
    },
  },
  mounted() {},
  created() {
    this.initOptions();
  },
};
</script>

<style lang="less">
</style>
